package hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * @author Edgar
 *
 */
@SuppressWarnings("deprecation")
public class HibernateUtil {
	public static final SessionFactory sessionFactory;
	public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
	
	static {
		try {
		// Create the SessionFactory from hibernate.cfg.xml
		sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
		
		}
		catch(Throwable e) {
			System.out.println("SessionFactory creation failed." + e);
			throw new ExceptionInInitializerError(e);
		}
	}
		
	public static Session currentSession() throws HibernateException {
		Session sess = (Session) session.get();
		// Open a new Session, if this thread has none yet
		if(sess == null){
		sess = sessionFactory.openSession();
		// Store it in the ThreadLocal variable
		session.set(sess);
		}
				
		return sess;
	}
	
	public static void SessionClose() throws Exception {
		Session s = (Session) session.get();
		if (s != null)
		s.close();
		session.set(null);
		}
	
}
